package com.lesserpand.springcloudalibabacore.test.controller;

import com.lesserpand.springcloudalibabacore.config.redis.redissonquery.utils.RedissonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author bailuo
 * @version 2.0
 * @Description 由于使用redisson实现分布式锁非常简单，这里直接写一个小栗子即可
 * @Date Created in 2019/1/14 14:30
 */
@RestController
@RequestMapping("/redisson-queue")
public class RedissonQueueTestController {

    @Autowired
    private RedissonUtils redissonUtils;

    /**
     * 发送延迟队列消息
     */
    @PutMapping(value = "/queue", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String queue(String message) {
        String queueName = "redis:delay:queue";
        Long timeout = 1L;
        return redissonUtils.sendMessageQueue(queueName, message, timeout, TimeUnit.MINUTES);
    }

    /**
     * 查询延迟队列消息
     *
     * @return
     */
    @GetMapping("/message")
    public String getMessage() {
        String queueName = "redis:delay:queue";
        return redissonUtils.getMessageQueue(queueName);
    }

}
